package akayblog;

import java.util.Date;

import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Button.ClickListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.DateField;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.RichTextArea;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;

public class EntryComponent extends CustomComponent {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private VerticalLayout mainLayout;

	private HorizontalLayout buttonLayout;

	private TextField entryHeader;

	private DateField entryIssueDate;

	private RichTextArea entryBody;

	private Button edit;

	private Button remove;

	private Button save;

	private MODE currentMode;

	private Entry entry;

	public EntryComponent() {
		this(new Entry());

	}

	public EntryComponent(Entry entry) {
		this.entry = entry;
		initMainLayout(entry);
	}

	public void setCurrentMode(MODE currentMode) {
		this.currentMode = currentMode;
		switch (currentMode) {
		case EDIT:
			edit.setVisible(false);
			remove.setVisible(false);
			save.setVisible(true);
			entryIssueDate.setVisible(false);
			entryHeader.setReadOnly(false);
			entryBody.setReadOnly(false);
			entryHeader.setCaption("Başlık");
			break;
		case READ:
			edit.setVisible(true);
			remove.setVisible(true);
			save.setVisible(false);
			entryIssueDate.setVisible(true);
			entryHeader.setReadOnly(true);
			entryBody.setReadOnly(true);
			entryHeader.setCaption("");
			break;
		default:
			break;
		}
	}

	public void initMainLayout(Entry entry) {

		mainLayout = new VerticalLayout();
		mainLayout.setSpacing(true);
		mainLayout.setWidth("100%");

		buttonLayout = new HorizontalLayout();
		buttonLayout.setSpacing(true);

		entryHeader = new TextField();
		entryHeader.setWidth("100%");

		setEntryIssueDate(new DateField());

		entryBody = new RichTextArea();

		save = new Button("Save");
		edit = new Button("Edit");
		remove = new Button("Remove");

		buttonLayout.addComponent(save);
		buttonLayout.setComponentAlignment(save, Alignment.TOP_LEFT);
		buttonLayout.addComponent(edit);
		buttonLayout.setComponentAlignment(edit, Alignment.TOP_LEFT);
		buttonLayout.addComponent(remove);
		buttonLayout.setComponentAlignment(remove, Alignment.TOP_LEFT);
		mainLayout.addComponent(getEntryIssueDate());
		mainLayout.addComponent(entryHeader);
		mainLayout.addComponent(entryBody);
		mainLayout.addComponent(buttonLayout);

		getEntryIssueDate().setValue(entry.getIssueDate());
		getEntryIssueDate().setImmediate(true);
		entryHeader.setValue(entry.getTitle());
		entryHeader.setImmediate(true);
		entryBody.setValue(entry.getText());
		entryBody.setImmediate(true);

		getEntryIssueDate().setReadOnly(true);

		setCurrentMode(MODE.READ);
		setCompositionRoot(mainLayout);
		addListeners();

	}

	private void addListeners() {

		edit.addListener(new ClickListener() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				setCurrentMode(MODE.EDIT);

			}
		});

		remove.addListener(new ClickListener() {
			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			@Override
			public void buttonClick(ClickEvent event) {
				
			}
		});

	}

	public Entry getEntry() {
		entry.setTitle((String) entryHeader.getValue());
		entry.setText((String) entryBody.getValue());
		entry.setIssueDate(new Date());

		return entry;
	}

	public static enum MODE {
		EDIT, READ
	}

	public VerticalLayout getMainLayout() {
		return mainLayout;
	}

	public HorizontalLayout getButtonLayout() {
		return buttonLayout;
	}

	public TextField getEntryHeader() {
		return entryHeader;
	}

	public RichTextArea getEntryBody() {
		return entryBody;
	}

	public Button getEdit() {
		return edit;
	}

	public Button getRemove() {
		return remove;
	}

	public Button getSave() {
		return save;
	}

	public MODE getCurrentMode() {
		return currentMode;
	}

	public DateField getEntryIssueDate() {
		return entryIssueDate;
	}

	public void setEntryIssueDate(DateField entryIssueDate) {
		this.entryIssueDate = entryIssueDate;
	}

}
